package com.open.system.service.impl;


import com.open.system.mapper.MenuMapper;
import com.open.base.model.Menu;
import com.open.common.model.Page;
import com.open.common.model.Sort;
import com.open.system.service.IMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 菜单信息
 *
 * @author 大树03
 * @date 2025-02-02 01:03
 */
@Service(value = "menuService")
@Slf4j
public class MenuServiceImpl implements IMenuService {

	private MenuMapper menuMapper;

    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }

    @Override
    @Transactional()
    public Boolean save(Menu model)  {
        return menuMapper.save(model);
    }

    @Override
    @Transactional()
    public Boolean update(Menu model)  {
        return menuMapper.update(model);
    }

	@Override
	public Page<Menu> findPage(Page<Menu> page,Menu item)  {
        Map<String,Object> map = new HashMap<String, Object>(4);
        int startPoint = (page.getIndex()-1)*page.getSize();
        map.put("orderBy",new Sort());
        map.put("item",item);
        map.put("startPoint",startPoint);
                map.put("endPoint",page.getSize());
                Integer count = menuMapper.findPageCount(map);
        if (count > startPoint) {
            page.setItems(menuMapper.findPage(map));
        }else {
            page.setItems(new ArrayList<Menu>());
        }
        page.setTotal(count);
        return page;
    }

	@Override
    public List<Menu> findByModel(Menu item)  {
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return menuMapper.findByModel(map);
    }

    @Override
    public List<Menu> findByModel(Map<String,Object> map)  {
        return menuMapper.findByModel(map);
    }

	@Override
	public Menu findByPk(Long pk)  {
        return menuMapper.findByPk(pk);
    }

    @Override
    public Integer countByModel(Menu item){
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return menuMapper.findPageCount(map);
    }

    @Override
    @Transactional()
    public Boolean remove(Long pk) {
        menuMapper.remove(pk);
        return true;
    }

    @Override
    public List<Menu> findByRoleId(Long roleId) {
        return menuMapper.findByRoleId(roleId);
    }

    @Override
    public List<Menu> findByMap(Map<String, Object> map) {
        return menuMapper.findByMap(map);
    }

    @Override
    public List<Menu> findAllMenu() {
        return menuMapper.findAllMenu();
    }
}